package cn.com.sd.system.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;

import cn.com.sd.common.util.MyStringUtils;
import cn.com.sd.common.util.Page;
import cn.com.sd.log.model.ServiceLog;
import cn.com.sd.log.service.IServiceLogService;


public class ServiceLogAction {

	HttpServletRequest request = ServletActionContext.getRequest();
	
	@Resource(name = "serviceLogService")
	private IServiceLogService serviceLogService;
	
	private Integer page;
	
	public String show() {
		
		int count = serviceLogService.getServiceLogCount();
		Page page = new Page(count, this.page);
		List<ServiceLog> serviceLogs = serviceLogService.list(page);
		
		request.setAttribute("serviceLogList", serviceLogs);
		request.setAttribute("page", page);
		return "show";
	}
	
	public String del() {
		String ids = request.getParameter("ids");
		if (StringUtils.isEmpty(ids)) {
			return "error";
		}
		
		List<Integer> idList = new ArrayList<Integer>();
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			if (StringUtils.isEmpty(id) || MyStringUtils.translateStringToInteger(id) == -1) {
				continue;
			}
			idList.add(MyStringUtils.translateStringToInteger(id));
		}
		if (CollectionUtils.isEmpty(idList)) {
			return "error";
		}
		if (idList.size() == 1) {
			serviceLogService.del(idList.get(0));
		} else {
			serviceLogService.delBanch(idList);
		}
		return show();
	}
	
	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}
}

